package com.geese.module.admin.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.common.constants.Constants;
import com.geese.common.entity.Permission;
import com.geese.common.entity.Role;
import com.geese.common.util.BeanUtil;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.exception.BusinessException;
import com.geese.module.admin.dto.RoleDto;
import com.geese.module.admin.vo.RoleQuery;
import com.geese.module.admin.vo.RoleVO;
import com.geese.module.security.dto.PermissionDto;
import com.geese.module.security.service.PermissionService;
import com.geese.module.security.service.RoleService;
import com.geese.util.CommonUtil;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author YeFangYi
 */
@Controller
@RequestMapping(value = "/admin")
@Scope("prototype")
public class RoleController extends BaseController {

	@Autowired
	private PermissionService permissionService;
	@Autowired
	private RoleService roleService;

	/**
	 * 获取资源列表
	 */
	@GetMapping("/menu/list")
	@ResponseBody
	public String listPermission() {
		List<Permission> permissionList = permissionService.listAll();
		return response(ImmutableMap.of("permissionList", CommonUtil.getPermissionList(permissionList)));
	}

	/**
	 * 新增角色
	 */
	@RequiresPermissions("role:create")
	@PostMapping("/role/save")
	@ResponseBody
	public String roleSave(@RequestBody RoleVO roleVo) {
		if (StringUtils.isEmpty(roleVo.getDescription())) {
			throw new BusinessException(130001);
		}
		List<String> tempIds = Splitter.on(",").trimResults().splitToList(roleVo.getPermissionIds());
    	if(CollectionUtils.isEmpty(tempIds)) {
    		throw new BusinessException(130002);
    	}
    	List<Integer> permissionIds = Lists.transform(checkIds(tempIds), new Function<String, Integer>() {
			@Override
			public Integer apply(String tempId) {
				return Integer.valueOf(tempId);
			}
		});
    	
    	Role role = BeanUtil.map(roleVo, Role.class);
    	roleService.save(role, permissionIds);
		return response();
	}
	
	/**
	 * 编辑角色
	 */
	@RequiresPermissions("role:update")
	@PostMapping("/role/update")
	@ResponseBody
	public String roleUpdate(@RequestBody RoleVO roleVo) {
		if(!StringUtils.isNumeric(roleVo.getId())) {
			throw new BusinessException(120009);
		}
		if (StringUtils.isEmpty(roleVo.getDescription())) {
			throw new BusinessException(130001);
		}
		List<String> tempIds = Splitter.on(",").trimResults().splitToList(roleVo.getPermissionIds());
    	if(CollectionUtils.isEmpty(tempIds)) {
    		throw new BusinessException(130002);
    	}
    	List<Integer> permissionIds = Lists.transform(checkIds(tempIds), new Function<String, Integer>() {
			@Override
			public Integer apply(String tempId) {
				return Integer.valueOf(tempId);
			}
		});
    	
    	Role role = BeanUtil.map(roleVo, Role.class);
    	roleService.update(role, permissionIds);
		return response();
	}
	
	/**
	 * 删除角色
	 */
	@RequiresPermissions("role:delete")
	@PostMapping("/role/remove")
	@ResponseBody
	public String roleDelete(@RequestParam String roleId) {
		if(!StringUtils.isNumeric(roleId)) {
			throw new BusinessException(120009);
		}
    	roleService.remove(Integer.valueOf(roleId));
		return response();
	}
	
	/**
     * 获取角色详情
     */
	@RequiresPermissions("role:view")
    @GetMapping("/role/find")
    @ResponseBody
    public String schoolList(@RequestParam String roleId) {
		if(!StringUtils.isNumeric(roleId)) {
			throw new BusinessException(120009);
		}
		RoleDto role = roleService.findById(Integer.valueOf(roleId));
        return response(ImmutableMap.of("role", role));
    }
	
	/**
     * 角色列表
     */
	@RequiresPermissions("role:view")
    @GetMapping("/role/listInPage")
    @ResponseBody
    public String schoolList(RoleQuery query) {
    	
    	Page page = getPageInfo(query);
    	Paginator<Role> pageList = roleService.listAllByQuery(query, page);
    	Map<String, Object> result = Maps.newHashMap();
    	result.put(Constants.PAGE_INFO, pageList.getPageResult());
    	result.put("roleList", pageList.getResults());
        return response(result);
    }

}
